﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
/*
 * User Properties
 * 
 * int Id
 * string Username
 * string Password
 * int PrivilegeLevel
 * 
 */
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// The user.
    /// Operations with data base.
    /// </summary>
    public class User : DalEntity
    {
        /// <summary>
        /// The DB - DAL properties mapping.
        /// Item1 - name,
        /// Item2 - dbName,
        /// Item3 - sqlParam,
        /// Item4 - type,
        /// Item5 - size
        /// </summary>
        private static List<Tuple<string, string, string, SqlDbType, int>> properties; 

        static User()
        {
            properties = new List<Tuple<string, string, string, SqlDbType, int>>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("C:\\Users\\Robert Sargsyan\\Documents\\Visual Studio 2012\\Projects\\Tracking\\UserArgs.xml");
            foreach (XmlNode arg in xmlDoc.DocumentElement)
            {
                properties.Add(new Tuple<string, string, string, SqlDbType, int>(arg.Attributes["name"].Value, arg.Attributes["dbName"].Value,
                    arg.Attributes["sqlParam"].Value,
                    (SqlDbType)Enum.Parse(typeof(SqlDbType), arg.Attributes["type"].Value),
                    Convert.ToInt32(arg.Attributes["size"].Value)));
            }
        }

        /// <summary>
        /// Returns a reader on all users
        /// </summary>
        public override Reader ReadAll(Dictionary<string, object> args)
        {
            SqlCommand command = new SqlCommand("ReadAllUsers");

            Reader reader = new Reader(command, properties);

            return reader;
        }

        /// <summary>
        /// Read the user by id
        /// </summary>
        public override Dictionary<string, object> ReadById(Dictionary<string, object> args)
        {
            return _readById("ReadUserById", properties, args, "user");
        }

        /// <summary>
        /// Creates a user in DB.
        /// </summary>
        public override Dictionary<string, object> Create()
        {
            return _create("CreateUser");
        }

        /// <summary>
        /// Writes the user in DB.
        /// </summary>
        public override bool? Write(Dictionary<string, object> args)
        {
            _write("WriteUser", args, properties);

            return true;
        }
        
        /// <summary>
        /// Removes the user from DB.
        /// </summary>
        public override bool? Remove(Dictionary<string, object> args)
        {
            _remove("RemoveUser", args);

            return true;
        }
    }
}
